package com.winchain.chainbill.bill.biz.transfer.controller;

import com.shell.bee.base.annotation.Logging;
import com.shell.bee.base.utils.JsonUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.persistence.model.PageLimit;
import com.shell.feign.PubDateFeign;
import com.shell.framework.utils.CommonFileUtil;
import com.shell.framework.utils.ParameterHelper;
import com.shell.governor.modules.vo.ParameterVo;
import com.winchain.chainbill.bill.base.po.CbXfe;
import com.winchain.chainbill.bill.base.service.CbAttService;
import com.winchain.chainbill.bill.base.service.CbXfeService;
import com.winchain.chainbill.bill.biz.query.service.BillQueryService;
import com.winchain.chainbill.bill.biz.query.vo.CbRecQueryVo;
import com.winchain.chainbill.bill.biz.query.vo.CbRecVo;
import com.winchain.chainbill.bill.biz.send.vo.BatchBillStats;
import com.winchain.chainbill.bill.biz.transfer.service.BillXfeQueryService;
import com.winchain.chainbill.bill.biz.transfer.service.BillXfeRevService;
import com.winchain.chainbill.bill.biz.transfer.service.BillXfeService;
import com.winchain.chainbill.bill.biz.transfer.vo.BillTransfer;
import com.winchain.chainbill.risk.utill.RateUtil;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class BillXfeController {
	@Autowired
	BillQueryService billQueryService;
	@Autowired
	BillXfeService billXfeService;
	@Autowired
	BillXfeQueryService billXfeQueryService;
	@Autowired
	BillXfeRevService billXfeRevService;
	@Autowired
	CbXfeService cbXfeService;
	@Autowired
	PubDateFeign dateFeign;
	@Autowired
	private CbAttService cbAttService;

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������������")
	public Map<String, Object> getInitData(String billId) {
		CbRecVo bill = this.billQueryService.selectBillDetail(billId);
		String grpId = bill.getGrpId();
		String financingRate = RateUtil.getRateByCode("financing_rate", "3", grpId, grpId, grpId);
		ParameterVo parameterVo = ParameterHelper.getParameterWithEditor("1", "BIZ_CONTROL_DELAY", bill

				.getSenderId(), bill.getGrpId());
		Map<String, Object> ret = new HashMap();
		ret.put("bill", bill);
		ret.put("financingRate", financingRate);
		ret.put("isCanDelay", parameterVo.getParmValue());
		ret.put("allHoliday", this.dateFeign.getHoliday("", ""));
		return ret;
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public RetMsg transfer(String billId, String phoneCode, String location, String cbTransferJson) throws Exception {
		List<CbXfe> cbXfeList = JsonUtil.fromJsonArray(cbTransferJson, CbXfe.class);
		CbXfe[] CbXfe = new CbXfe[0];
		CbXfe = (CbXfe[]) cbXfeList.toArray(CbXfe);
		this.billXfeService.transfer(billId, phoneCode, location, CbXfe);
		return new RetMsg("success", false, "����������");
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "����������������")
	public List<CbRecVo> selectBillForXfeReviewByPage(PageLimit limit, CbRecQueryVo cbRecordQueryVo) {
		return this.billXfeQueryService.selectBillForXfeReviewByPage(limit, cbRecordQueryVo);
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������")
	public RetMsg reviewPass(String phoneCode, String location, String billIds) throws Exception {
		if (StringUtil.emptyAndNull(billIds)) {
			new RetMsg("error", false, "��������");
		}
		String[] billIdArr = billIds.split(",");
		BatchBillStats<CbXfe> stats = this.billXfeRevService.pass(true, phoneCode, location, billIdArr);
		if (billIdArr.length == stats.getAffectedRows()) {
			return new RetMsg("success", false, "��������������������������������");
		}
		if (stats.getAffectedRows() == 0) {
			return new RetMsg("error", false, "������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
		}
		if (stats.getAffectedRows() == -1) {
			return new RetMsg("error", false, "��������������������������");
		}
		return new RetMsg("warning", false, "����������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "������������")
	public RetMsg reviewReject(String remark, String billIds) throws Exception {
		if (StringUtil.emptyAndNull(billIds)) {
			new RetMsg("error", false, "��������");
		}
		String[] billIdArr = billIds.split(",");
		BatchBillStats<CbXfe> stats = this.billXfeRevService.reject(remark, billIdArr);
		if (billIdArr.length == stats.getAffectedRows()) {
			return new RetMsg("success", false, "����������������������������������");
		}
		if (stats.getAffectedRows() == 0) {
			return new RetMsg("error", false, "����������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
		}
		return new RetMsg("warning", false, "��������������������", CommonFileUtil.changMsg(stats.getAbnormalMsg()));
	}

	@RequestMapping
	@ResponseBody
	@Logging(module = "��������", methods = "��������")
	public Map<String, Object> getDetail(String billId) {
		CbRecVo bill = this.billQueryService.selectBillDetail(billId);
		List<BillTransfer> transfers = this.cbXfeService.selectSubXfesBillByPrntId(billId);
		this.cbAttService.setAtt(transfers);
		Map<String, Object> ret = new HashMap();
		if ((transfers != null) && (transfers.size() > 0)) {
			BigDecimal totalTransfer = new BigDecimal(0);
			for (BillTransfer t : transfers) {
				totalTransfer = totalTransfer.add(t.getBillAmt());
			}
			ret.put("overplusAmt", bill.getBillAmt().subtract(totalTransfer));
		} else {
			ret.put("overplusAmt", Integer.valueOf(0));
		}
		ret.put("bill", bill);
		ret.put("transfers", transfers);
		return ret;
	}
}
